All right.
Welcome.
In this lecture, I'm going to show you how to create a simple day and night cycling system where you
can adjust the duration of the day into how many seconds you'd like the day to last.
This is going to be pretty simple.
All we need to do is create a new script in server script service.
I'm just going to name this script day night cycle.
And in here, we're going to need a few things.
We're going to need access to some services.
We're going to need some constants.
We'll need a variable.
And then we're going to need a listener to listen to an event.
So we'll need an event handler.
Now, the very first service we need to get is the lighting service.
So we'll grab it here in this variable.
And then we're also going to need another service called the run service.
Run service.
After that, we're going to define a few constants here.
One is going to be called day length.
And this is going to represent how long we need the day to be in seconds.
So I'll just set it to 240 seconds and then we'll need a constant for what?
The hour we'd like to start the day at.
So start hour.
And we could set it to be something like maybe 4 a.m. We're going to need to define a constant called
max brightness.
And I'm going to set it to three because in the lighting service there is a brightness property here
and we're going to be adjusting this throughout the day.
So when it's in the morning and in the middle of the day and the afternoon, it'll be at three.
And then when it's at nighttime in the evening, we'll set it to zero.
And then the last thing we need is something called brightness delta.
And we're going to get this by dividing the max brightness by our day length.
And this is because we're going to be accessing an event in the run service called Heartbeat, which
fires multiple times per second, and we're going to be incrementing and decrementing the brightness
property over time as our time elapses and we're going to add and subtract to it with this brightness
delta, which I'm just dividing by our day length to get, you know, kind of a general delta for subtracting
the brightness.
Because let's say if I define a constant brightness delta, if I went and changed the day length to
something like maybe ten seconds and I had a constant brightness delta here, well, we might not be
able to reach our brightness all the way at three or all the way down to zero based on whatever I have
defined in here.
Or maybe if I have this set at like maybe 10,000 seconds, then in the brightness delta might be too
large of a number and it might shift the brightness too quickly while it's still sunny or while it's
still nighttime out or whatever.
So I'm just dividing max brightness by the day length to just kind of get a general number we can use
that'll be proportional to our day length.
And I'm going to need a variable here.
I'll call it start.
And it's going to be equal to we're going to get the tick function and we're going to subtract from
it.
We're going to get the start our we're going to divide the start our by 24 to give us a fraction.
And then we're going to multiply this by the day length.
So what I'm doing here is I'm getting what fraction?
Right.
What percentage of our day length is equal to what?
Of our star?
Our is so our star.
Our is four divided by 24.
So what's four?
24th of our day length.
We get that number and then we subtract it from the tick and we're going to be using Tick to keep track
of how much time has elapsed.
Next, we're going to get our own service and we're going to listen to the Heartbeat event and we'll
connect a function to this.
And in here, we're going to create another two variables.
One, we'll call time elapsed, and this one's going to be equal to the current tick subtracted by our
start time.
So as the time goes on, since Tick returns a number along with decimals, we'll be able to see exactly
how many seconds has elapsed every time this event gets fired.
And we know that because we're subtracting the start time that we made here.
So it'll give us the exact number of how many seconds elapsed.
So that way, you know, since our start hour is at four, we know that this would return 40 seconds
out of 240.
So this will give us a difference of 40 seconds.
So this is telling us, hey, 40 seconds has elapsed so far.
Now, the problem with this is that we don't need seconds.
We need hours because in the lighting service, we need to be able to set the clock time property to
a number that represents hours.
So how do we convert these seconds to hours?
Well, we can create another variable, we'll call it hour of day, and it's going to be equal to we
are going to get the current time elapsed and we're going to divide it by the day length to give us
a fraction.
And then we're just going to multiply that by 24.
So if the time elapsed is 40 seconds, well, 40 divided by 240 gives us a percentage of that day and
we just multiply that to 24 to give us the exact number or percentage that represents that in 24 hours.
So we get the correct hour of day.
Now that we have done that, we're going to create a third variable in here.
I'll just call it target brightness, and I'm not going to initialize anything or assign a value yet
because I want to check if the hour of day is greater than, let's say, seven.
So seven in the morning.
And we also want to make sure the hour of day is less than or equal to 17, which is about 5:00 or 5:00
pm in the afternoon.
If it is within that time range, then we want to set the target brightness to three because it's in
the middle of the day.
Otherwise, if it's not within that time frame, it's either night time or it's in the evening or it's
in the very early morning.
That means we want to set the target brightness to zero.
Now that we have calculated our target brightness, then we can go down here and check if the lighting
service dot brightness, if it's less than our target brightness.
So let's say our target brightness is three and the brightness is zero, then we want to increase the
brightness, right?
So we'll do lighting service.
Dot brightness is going to be equal to and we're going to use the math dot minimum function.
And this function returns a number.
You give it a bunch of numbers and it'll return to you the number that is least out of all those numbers.
So we want to pass our target brightness because that's the max amount of brightness we want.
We don't want to go over that.
And then we're going to pass the lighting service dot brightness, plus this brightness delta.
So every time this function keeps getting run, we're going to keep incrementing the brightness of our
lighting service until we reach this target brightness.
We'll return that and we'll set that brightness to three.
And then once that happens, this if statement doesn't run anymore because brightness is equal to the
target brightness.
Now, we also need to do this for when our target brightness is zero, so we can check if the lighting
service dot brightness is greater than our target brightness.
So let's say it's at three and we need to set it to zero.
Then we can do the same thing here.
We're going to set it equal to math dot max, which returns the maximum number out of the numbers you
pass to the function.
And again, we're going to pass target brightness, which would be zero, and we're going to do lighting
service, dot brightness, subtract it by this brightness delta.
So just in case it subtracts more than it needs to and it sets the brightness to be negative, it'll
set it back to zero.
That way we never go below zero and we never go above three here.
And then the last thing we need to check is if the time elapsed becomes greater than we can do, greater
than or equal to the day length.
So once we have surpassed 240 seconds, well, what we need to do is we need to set the start variable
back to the current time and we can do that by just doing start is equal to tick.
So now that we have reset the start time, when we go back to our time elapsed, it'll give us the correct
time.
So of course, tick minus, tick would be zero.
So zero seconds have elapsed and we would continue elapsing the time as tick goes on.
And then the last very last thing we need to do is we need to set the lighting service clock time to
the hour of day and that's it.
We have built a simple day night cycle, so let's go over it one more time just to make sure you understand
everything in this constant.
We're defining how long we want the day to be in seconds.
It is 240 seconds.
We're defining what we want the time to start at.
So when the game first runs, we want the time to start at 4 a.m. This number defines us.
The maximum brightness we'll need in the game, which we divide by the day length to give us a brightness
delta that we use to increase and decrease the current brightness in our game.
And actually, since this is the same number here, what we could do is we could just set this to max
brightness and actually let's create another constant, we'll call it min brightness.
So the minimum brightness, oops, brightness and we'll set it to zero.
And then we can set that to the minimum brightness.
That way we don't have any magic, random floating numbers around.
So once we have these constants acquired, we go down and create a variable called START.
And it's basically just the current time subtracted by our day length based on our current hour.
So we get the fraction out of 24 hours of our start hour.
So four divided by 24 and we multiply that to day length to give us the correct percentage or how many
seconds of our day length is equal to 4/24.
And then once we have this start, we go into our event here, we subtract tick by our current start
time.
So we know the time elapsed is 40 seconds and then we calculate the hour of day based on that, which
would be four hours.
And then we set the target brightness based on what time it is.
So if it's in the morning, the target brightness is going to be three.
If it's in the evening or at night time, then it'll be at zero.
And based on that, we increase or decrease the brightness over time.
And then once our time elapse becomes greater than or equal to our day length, then we reset the start
back to tick and then tick.
Divided by tick is zero.
So that starts at a new day.
And then finally, we set the clock time equal to the hour of day we calculated up here.
So the last thing we need to do is to test to see if it works so we can go and run our game and we can
check to see.
Well, look at that.
It's currently nighttime and let's go ahead and look at the moon.
And there you go.
You see, our moon is slowly moving.
Once it reaches the horizon, we should see the brightness increase on our map.
Well, actually, we can monitor it.
Monitor it from here.
So pretty soon our brightness should start to go up.
There we go.
Our brightness is going up.
And now our son is going to slowly move until the total day or 24 hours is equal to 240 seconds.
And then we restart and start a new day.
So we can see our sun slowly moving there.
Now, if we want to make the day faster, we can set it to something like 30 seconds.
And we don't need to change anything else because remember, we're calculating everything here using
math.
There's no defined constants for the brightness delta.
We don't have to mess with any of these variables.
It's all based on our day length.
So if we run the game again.
We should see that it's definitely much faster.
And that also means our brightness should go down quicker as well because our time is moving faster.
So there we go, already at zero.
And then the moon's up.
It's coming around.
Moon goes to the horizon and then the sun comes up and boom, we're back at three brightness.
And it just continues this on and on.
So as you can see, it's pretty simple to make a day night cycle in your game really only takes about
38 lines of code.
And in here, you can also continue to change other things in the lighting properties like you can change
maybe the ambient color, you could try to change, maybe the diffuse and the specular numbers.
You could try changing the color shift properties, kind of do miss around whatever you'd like in this
event.
So I hope you enjoyed it and I'll see you in the next lecture.